#_*_encoding :utf-8_*_
from com.zjs.crawer.urlcontent.baseurlcontent import BaseUrlContent
from com.zjs.zjsqueue import zurlcontentqueue
from bs4 import BeautifulSoup as BS
from com.zjs.util.taobao_download import request
from com.zjs.util.converttolocation import location
from com.zjs.util.write import write_content
import re,logging
import threading
import json
class TaoBaoContent(BaseUrlContent):
    name = "taobao"
    def __init__(self):
        logging.debug("[content][taobao]:start!")

    def run(self):
        while True:
            try:
                threads = []
                for i in range(10):
                    url = zurlcontentqueue.get(self.name)
                    logging.debug("[content]["+self.name+"]:"+url)
                    threads.append(threading.Thread(target=self.get_content_,args=(url,)))
                    
                for t in threads:
                    t.start()
         
                for thread in threads: 
                    thread.join() 
                      
            except Exception as ex:
                logging.error("[content]["+self.name+"]:"+str(url)+"解析失败！" )
                logging.exception(ex, exc_info=1) 

    def get_content_(self,url):  
        result = {}
        html=request.get(url,3)
        soup=BS(html.text,"lxml")
        script_con=soup.find_all("script")
        need_con=script_con[5].text
        need_con=need_con.strip()
        text_list=need_con.split("g_srp_loadCss()")
        g_page_config=text_list[0].strip()
        g_page_config=g_page_config.replace("\n","")
        page_list=g_page_config.split("=")
        storelist=page_list[1]         
        storelist=storelist[:-1]    #去掉最后一个;
        dict_json=json.loads(storelist)
        for d1,x1 in dict_json.items():     #遍历第一层数据
            '''d为key  x为value'''
            if d1=="mods":
                for d2,x2 in x1.items():     #遍历第二层数据 
                    if d2=="shoplist":                           
                        for d3,x3 in x2.items():    #遍历第三层数据  
                            if d3=="data":                                              
                                for d4,x4 in x3.items():    #遍历第四层数据  
                                    if d4=="shopItems":                                  
                                        for shop in x4:    #x4为list类型  遍历第五层数据    倒数第二层数据
                                            result={}
                                            for d5,x5 in shop.items():   #shop为字典类型  每个店铺的详细情况 
                                                if d5=="uid":
                                                    result["店铺ID"]=x5
                                                elif d5=="title":
                                                    result["店铺名"]=x5
                                                elif d5=="nick":
                                                    result["卖家昵称"]=x5
                                                elif d5=="provcity":                                                                               
                                                    if x5!=None and x5!="":
                                                        if len(x5)<=3:  #表示值只有市      
                                                            result["市区"]=x5
                                                            result["省份"]=""
                                                        else:
                                                            loc=x5.split(" ")
                                                            result["省份"]=loc[0]
                                                            result["市区"]=loc[1]                                  
                                                elif d5=="totalsold":
                                                    result["总销量"]=x5
                                                elif d5=="procnt":
                                                    result["商品总数"]=x5
                                                elif d5=="dsrInfo":
                                                    for d6,x6 in x5.items():    #遍历最后一层'
                                                        if d6=="mgDomClass": #描述相符高于还是低于同行业  morethan 是高于  lessthan是低于
                                                            if x6.find("morethan"):
                                                                result["描述"]="高于同行业"
                                                            else:
                                                                result["描述"]="低于同行业"
                                                        elif d6=="dsrStr":                                                     
                                                            dict_x6=json.loads(x6)
                                                                                                               
                                                            for d7,x7 in dict_x6.items():
                                                                if d7=="mg":
                                                                    result["描述相符比"]=x7
                                                                elif d7=="cg":
                                                                    result["物流服务比"]=x7
                                                                elif d7=="sg":
                                                                    result["服务态度比"]=x7
                                                                elif d7=="cas":
                                                                    result["物流评分"]=x7
                                                                elif d7=="sgr":
                                                                    result["好评率"]=x7
                                                                elif d7=="sas":
                                                                    result["服务评分"]=x7
                                                                elif d7=="mas":
                                                                    result["描述评分"]=x7
                                                                else :
                                                                    result[d7]=x7
                                                        elif d6=="cgDomClass":
                                                            if x6.find("morethan"):
                                                                result["服务"]="高于同行业"
                                                            else:
                                                                result["服务"]="低于同行业"
                                                        if d6=="sgDomClass": 
                                                            if x6.find("morethan"):
                                                                result["物流"]="高于同行业"
                                                            else:
                                                                result["物流"]="低于同行业"
                                            write_content("taobao_new_shuma", result)

   
